package com.ruiyian.bridge.project.controller;

import com.ruiyian.bridge.project.domain.DeviceType;
import com.ruiyian.bridge.project.service.DeviceTypeService;
import com.ruiyian.common.annotation.Log;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.common.enums.BusinessType;
import com.ruiyian.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 传感器类型Controller
 *
 * @author ruiyian
 * @date 2022-01-20
 */
@RestController
@RequestMapping("/bridge/project/sensorType")
public class DeviceTypeController extends BaseController
{
    @Autowired
    private DeviceTypeService tDeviceTypeService;

    /**
     *
     * @param deviceType
     * @return
     * 获取设备列表，下拉框对设备进行查询
     */
    @GetMapping("/list")
    public TableDataInfo list(DeviceType deviceType)
    {
        startPage();
        List<DeviceType> list = tDeviceTypeService.selectTSensorTypeList(deviceType);
        return getDataTable(list);
    }

    /**
     * 查询所有
     * @param deviceType
     * @return
     */
    @PostMapping("/getListAll")
    public AjaxResult getListAll(@RequestBody DeviceType deviceType)
    {
        List<DeviceType> list = tDeviceTypeService.selectTSensorTypeList(deviceType);
        return AjaxResult.success(list);
    }

    /**
     * 导出传感器类型列表
     */
    @Log(title = "传感器类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceType deviceType)
    {
        List<DeviceType> list = tDeviceTypeService.selectTSensorTypeList(deviceType);
        ExcelUtil<DeviceType> util = new ExcelUtil<DeviceType>(DeviceType.class);
        try {
            util.exportExcel(response, list, "传感器类型数据");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取传感器类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('bridge:project:sensorType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tDeviceTypeService.selectTSensorTypeById(id));
    }

    /**
     * 新增传感器类型
     */
    @Log(title = "传感器类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceType deviceType)
    {
        return toAjax(tDeviceTypeService.insertTSensorType(deviceType));
    }

    /**
     * 修改传感器类型
     */
    @Log(title = "传感器类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceType deviceType)
    {

        return toAjax(tDeviceTypeService.updateTSensorType(deviceType));
    }

    /**
     * 删除传感器类型
     */
    @Log(title = "传感器类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDeviceTypeService.deleteTSensorTypeByIds(ids));
    }
}
